import Vue from "vue";
import vueRouter from "vue-router";
import Nprogress from "nprogress";
import "nprogress/nprogress.css";
Vue.use(vueRouter);
import data from "@/views/data";
import login from "@/views/login";
import index from "@/views/index";
import companylist from "@/views/companylist";
import subjecklist from "@/views/subjecklist";
import userlist from "@/views/userlist";
import worklist from "@/views/worklist";
import store from "@/store/index.js";
import { removeToken } from "@/utils/token.js";
import { Message } from "element-ui";
const router = new vueRouter({
  routes: [
    {
      path: "/",
      redirect: "/login",
      meta: {
        role: ["超级管理员", "管理员", "老师", "学生"],
        title: "登录",
      },
    },
    {
      path: "/login",
      component: login,
      meta: {
        role: ["超级管理员", "管理员", "老师", "学生"],
        title: "登录",
      },
    },
    {
      path: "/index",
      component: index,
      redirect: "/subjecklist",
      children: [
        {
          path: "/data",
          component: data,
          meta: {
            role: ["超级管理员", "管理员", "老师"],
            icon: "el-icon-pie-chart",
            title: "数据概览",
          },
        },
        {
          path: "/userlist",
          component: userlist,
          meta: {
            role: ["超级管理员", "管理员"],
            icon: "el-icon-user",
            title: "用户列表",
          },
        },
        {
          path: "/worklist",
          component: worklist,
          meta: {
            role: ["超级管理员", "管理员", "老师"],
            icon: "el-icon-edit-outline",
            title: "跟库列表",
          },
        },
        {
          path: "/subjecklist",
          component: subjecklist,
          meta: {
            role: ["超级管理员", "管理员", "老师", "学生"],
            icon: "el-icon-office-building",
            title: "学科列表",
          },
        },
        {
          path: "/companylist",
          component: companylist,
          meta: {
            role: ["超级管理员", "管理员", "老师"],
            icon: "el-icon-notebook-2",
            title: "企业列表",
          },
        },
      ],
    },
  ],
});
router.beforeEach((fo, from, next) => {
  console.log("fo", fo);
  console.log("from", from);
  Nprogress.start();
  //根据用户类别判断权限
  if (fo.meta.role.includes(store.state.role)) {
    console.log("用户", store.state.role);
    next();
  } else {
    removeToken();
    router.push("/login");
    Message.error("没有访问该页面的权限");
  }
});
router.afterEach((fo) => {
  document.title = fo.meta.title;
  Nprogress.done();
});
export default router;
